package com.example.homework;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;

public class HomeActivity extends AppCompatActivity {

    private boolean isImage = true; // 设置切换图片的标志位

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        Button nativeRequestButton = findViewById(R.id.home_button_1);
        Button okhttpRequestButton = findViewById(R.id.home_button_2);
        Button glideImageButton = findViewById(R.id.home_button_3);
        Button exoPlayerButton = findViewById(R.id.home_button_4); // 这是播放视频按钮
        ImageView imageView = findViewById(R.id.home_image_view);

        // 原生请求网页
        nativeRequestButton.setOnClickListener(v -> {
            NativeHttpConnection nativeHttp = new NativeHttpConnection();
            nativeHttp.fetchPageContent("https://ts1.cn.mm.bing.net/th/id/R-C.3eb1e411d32f4012926fa8b6a277b5ad?rik=GQECjupxOmgdyw&riu=http%3a%2f%2fseopic.699pic.com%2fphoto%2f50064%2f9004.jpg_wh1200.jpg&ehk=FzurNGE3IJFVI6FPo2BOddmWpijbe9A459YDPl7dwUU%3d&risl=&pid=ImgRaw&r=0");
            Log.d("请求网页", "原生请求网页成功");
        });

        // OkHttp请求网页
        okhttpRequestButton.setOnClickListener(v -> {
            OkHttpConnection okHttp = new OkHttpConnection();
            okHttp.fetchPageContent("https://img95.699pic.com/photo/50048/7510.jpg_wh860.jpg");
            Log.d("请求网页", "OkHttp请求网页请求成功");
        });

        // 使用Glide加载图片，并切换图片
        glideImageButton.setOnClickListener(v -> {
            GlideImageLoader glideLoader = new GlideImageLoader();
            String imageUrl;

            if (isImage) {
                imageUrl = "https://ts1.cn.mm.bing.net/th/id/R-C.3eb1e411d32f4012926fa8b6a277b5ad?rik=GQECjupxOmgdyw&riu=http%3a%2f%2fseopic.699pic.com%2fphoto%2f50064%2f9004.jpg_wh1200.jpg&ehk=FzurNGE3IJFVI6FPo2BOddmWpijbe9A459YDPl7dwUU%3d&risl=&pid=ImgRaw&r=0";
            } else {
                imageUrl = "https://img95.699pic.com/photo/50048/7510.jpg_wh860.jpg";
            }

            glideLoader.loadImage(imageUrl, imageView);
            isImage = !isImage; // 切换标志位
        });

        // 使用ExoPlayer播放视频
        exoPlayerButton.setOnClickListener(v -> {
            // 跳转到视频播放页面
            Intent intent = new Intent(this, VideoActivity.class);
            startActivity(intent);
        });
    }
}
